//
//  FR_PersonalViewController.m
//  FaceRecognition
//
//  Created by Sylar on 2016/11/28.
//  Copyright © 2016年 Sylar. All rights reserved.
//

#import "FR_PersonalViewController.h"
#import "Persnoal_Information_ViewController.h"
#import "ChangeRoleViewController.h"
#import "WebViewController.h"
#import "AccountAndSafetyViewController.h"
#import "AboutAppViewController.h"
#import "FR_Feedback_ViewController.h"


@interface FR_PersonalViewController ()<UITableViewDelegate, UITableViewDataSource,UINavigationBarDelegate>
{
    SYAccount *accountDic;
    
}

@property (nonatomic, strong) UITableView *myTable;

@end

@implementation FR_PersonalViewController

- (id)init
{
    self = [super init];
    if (self) {
        
        self.hidesBottomBarWhenPushed = NO;
        
    }
    return self;
}

- (void)loadView {
    [super loadView];
    
    self.navBar.nav_titleLabel.text = @"我的";
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    
    //[[NSNotificationCenter defaultCenter] postNotificationName:@"judgeLoginState" object:nil];
    //1.获取个人信息
    accountDic=[SYAccountTool ShareAccount].account;
    NSIndexSet *set = [NSIndexSet indexSetWithIndex:0];
    [self.myTable reloadSections:set withRowAnimation:UITableViewRowAnimationNone];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.myTable];
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITabelViewDataSource & UITabelViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0) {
        
        return 1;
    }else if (section == 1) {
        return 4;
    }else {
        return 1;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 12;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    if (section == 1) {
        UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 12)];
        
        footView.backgroundColor = colorFromHex(@"#F4F5F7");
        
        return footView;
    }
    
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    if (indexPath.section == 0) {
        return 65;
    }else {
        return 44;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    NSInteger section = indexPath.section;
    NSInteger row = indexPath.row;
    UIImageView *leftImage;
    UILabel *titleLabel;
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellIdentifier"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cellIdentifier"];
        
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    if (indexPath.section == 0) {
        
        leftImage = [[UIImageView alloc]initWithFrame:CGRectMake(15, 10, 45, 45)];
        leftImage.tag = 50;
        leftImage.layer.cornerRadius = 22.5;
        leftImage.clipsToBounds = YES;
        leftImage.contentMode = UIViewContentModeScaleAspectFill;
        NSString *logo = accountDic.logo;
        if (![logo hasPrefix:@"http"] && logo) {
            logo = [REQUESTIMAGEURL stringByAppendingString:logo];
        }
        [leftImage sd_setImageWithURL:[NSURL URLWithString:logo] placeholderImage:[UIImage imageNamed:@"personalsettings_switchloading_pic_"]];
        
        for (UIView *view in cell.contentView.subviews) {
            
            [view removeFromSuperview];
        }
        
        [cell.contentView addSubview:leftImage];
        
        titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(leftImage.right+10, 25, 150, 16)];
        titleLabel.tag = 51;
        [titleLabel setFont:[UIFont systemFontOfSize:16]];
        [titleLabel setTextColor:colorFromHex(@"000000")];
        titleLabel.text = accountDic.username;
        [cell.contentView addSubview:titleLabel];
        
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 76.5, SCREENWIDTH, 0.5)];
        [line setBackgroundColor:colorFromHex(@"#DEDEDE")];
        
        [cell.contentView addSubview:line];
        
    }else if (section == 1) {
        
        leftImage = [[UIImageView alloc]initWithFrame:CGRectMake(15, 13, 18, 18)];
        leftImage.tag = 50;
        [cell.contentView addSubview:leftImage];
        
        titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(leftImage.right+5, 14, 150, 16)];
        titleLabel.tag = 51;
        [titleLabel setFont:[UIFont systemFontOfSize:16]];
        [titleLabel setTextColor:colorFromHex(@"3b3b3b")];
        [cell.contentView addSubview:titleLabel];
        
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(15, 43.5, SCREENWIDTH, 0.5)];
        [line setBackgroundColor:colorFromHex(@"#DEDEDE")];
        
        [cell.contentView addSubview:line];
        
        if (row == 0) {
            [leftImage setImage:[UIImage imageNamed:@"xiugaimima"]];
            titleLabel.text = @"账户与安全";
        }else if (row == 1) {
            [leftImage setImage:[UIImage imageNamed:@"shiyongshuoming"]];
            titleLabel.text = @"使用说明";
            
            
        }else if (row == 2) {
            [leftImage setImage:[UIImage imageNamed:@"wentifankiu"]];
            titleLabel.text = @"问题反馈";
            
            
        }else if (row == 3) {
            [leftImage setImage:[UIImage imageNamed:@"guanyu"]];
            titleLabel.text = @"关于师云平安幼儿";
            
            //最后一行分割线的X坐标，以后要加行的话记得修改
            [line setX:SCREENWIDTH];
        }
        
    }else if (section == 2) {
        
        cell.accessoryType = UITableViewCellAccessoryNone;
        
        UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 44)];
        [lab setTextColor:colorFromHex(@"f55f5f")];
        [lab setFont:[UIFont systemFontOfSize:17]];
        [lab setText:@"退出登录"];
        [lab setTextAlignment:NSTextAlignmentCenter];
        
        [cell.contentView addSubview:lab];
        
        
    }
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        Persnoal_Information_ViewController *perVC = [[Persnoal_Information_ViewController alloc]init];
        
        [self.navigationController pushViewController:perVC animated:YES];
    }else if (indexPath.section == 1) {
        
        if (indexPath.row == 0 ) {
            
            AccountAndSafetyViewController *accVC = [[AccountAndSafetyViewController alloc]init];
            [self.navigationController pushViewController:accVC animated:YES];
            
        }else if(indexPath.row == 1) {
            
            WebViewController *web = [[WebViewController alloc]init];
            
            web.requestURL = @"http://apichild.our100.net/about/payesysm/sysm.html";
            
            [self.navigationController pushViewController:web animated:YES];
            
        }else if(indexPath.row == 2) {
            
            FR_Feedback_ViewController *vc = [[FR_Feedback_ViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
            
        }else if(indexPath.row == 3) {
            
            AboutAppViewController *aboutVC = [[AboutAppViewController alloc]init];
            
            [self.navigationController pushViewController:aboutVC animated:YES];
            
        }
    }else if (indexPath.section == 2) {
        [self logoutAction];
    }
}


//退出登录
- (void)logoutAction{
    NSDictionary * imformation = [SyUserInfomation acquirePersonalImformationFromMemoryWithPersonID:USERID];
    NSDictionary *userInfo = [imformation objectForKey:@"userinfo"];
    NSString *lasttype = [userInfo objectForKey:@"lasttype"];
    NSMutableDictionary * dict = [[NSMutableDictionary alloc] initWithCapacity:0];
    [dict setObject:@"api_user_logout" forKey:@"method"];
    [dict setObject:USERID forKey:@"userid"];
    if (lasttype) {
        
        [dict setObject:lasttype forKey:@"usertype"];
    }
    [Utils get:nil parameters:dict success:^(NSInteger recode, id resposeJson){
        //NSDictionary *dict = (NSDictionary *)resposeJson;
        //[Utils alertViewWithMessage:[dict objectForKey:@"re_info"]];
        NSUserDefaults *udd=[NSUserDefaults standardUserDefaults];
        [udd removeObjectForKey:@"studentID"];
        [udd removeObjectForKey:SYStudentName];
        [udd removeObjectForKey:@"userid"];
        [udd synchronize];
        AppDelegate * appdelegate = CONSTAPPDELEGATE;
        appdelegate.studentId = nil;
        appdelegate.myClasses = nil;
        
        [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_LOGINCHANGE object:@NO];
        
        [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
        
        
    } failure:^(NSInteger recode, NSError * error){
        [self showHint:error.localizedDescription];
    }];
}
#pragma mark - Get 

- (UITableView *)myTable {
    
    if (_myTable == nil) {
        
        _myTable = [[UITableView alloc]initWithFrame:CGRectMake(0, SafeAreaTopHeight, SCREENWIDTH, SCREENHEIGH - SafeAreaTopHeight - SafeAreaBottomHeight) style:(UITableViewStylePlain)];
        _myTable.delegate = self;
        _myTable.dataSource = self;
        _myTable.scrollEnabled = NO;
        _myTable.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_myTable setBackgroundColor:colorFromHex(@"F4F5F7")];

    }
    
    return _myTable;
}

- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPushItem:(UINavigationItem *)item {
    
        return YES;
    
}

- (void)navigationBar:(UINavigationBar *)navigationBar didPopItem:(UINavigationItem *)item {
    
    
    
}


@end
